import CryptoJS from 'crypto-js'
import Cookies from 'js-cookie'
const s1 = Cookies.get('s1')
const s2 = Cookies.get('s2')
const s3 = Cookies.get('s3')
const s4 = Cookies.get('s4')
export default {

  // 随机生成指定数量的16进制key

  // 加密
  encrypt(word) {
    const result = []
    for (var i = 0, len = s1.length; i < len; i += 2) {
      result.push(String.fromCharCode(parseInt(s1.slice(i, i + 2), 16)))
    }
    const result1 = []

    for (var i = 0, len = s3.length; i < len; i += 2) {
      result1.push(String.fromCharCode(parseInt(s3.slice(i, i + 2), 16)))
    }

    const keyStr = `${result.join('')}${s2}`
    const ivStr = `${result1.join('')}${s4}`
    console.log(keyStr)
    const key = CryptoJS.enc.Utf8.parse(keyStr)
    const iv = CryptoJS.enc.Utf8.parse(ivStr)
    const srcs = CryptoJS.enc.Utf8.parse(word)

    const encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding })
    console.log(encrypted.ciphertext.toString().toUpperCase())
    return encrypted.ciphertext.toString().toUpperCase()
    // let encrypted = CryptoJS.AES.encrypt(srcs, key, {
    // iv,
    // mode: CryptoJS.mode.CBC,
    // padding: CryptoJS.pad.ZeroPadding
    // });
    // return encrypted.toString();
  },
  // 解密
  decrypt(word, keyStr, ivStr) {
    keyStr = keyStr || '12345678901234567890123456789012'
    ivStr = ivStr || 'absoietlj32fai12'
    var key = CryptoJS.enc.Utf8.parse(keyStr)
    const iv = CryptoJS.enc.Utf8.parse(ivStr)

    var decrypt = CryptoJS.AES.decrypt(word, key, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.ZeroPadding
    })
    return decrypt.toString(CryptoJS.enc.Utf8)
  }
}
